package com.cssw.fyzb.tps.service.im;

import com.cssw.fyzb.tps.service.im.common.ImHttpComm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Jian Qin.
 */
@Service
public class PushServiceImpl implements PushService{
    @Autowired
    private ImHttpComm imHttpComm;

    @Override
    public ImHttpComm.ImResult setAttrName(String[] indexes, String[] attrNames) {
        Map<String, Object> params = new HashMap<>();
        Map<String,String> attrs = new HashMap<>();
        for(int i = 0; i < indexes.length; i++){
            attrs.put(indexes[i], attrNames[i]);
        }
        params.put("AttrNames", attrs);
        return imHttpComm.post("/all_member_push/im_set_attr_name", params);
    }

    @Override
    public ImHttpComm.ImResult getAttrName() {
        return imHttpComm.post("/all_member_push/im_get_attr_name", new HashMap());
    }

    @Override
    public ImHttpComm.ImResult getAttr(String[] to_accounts) {
        Map<String, Object> params = new HashMap<>();
        params.put("To_Account", to_accounts);
        return imHttpComm.post("/all_member_push/im_get_attr", params);
    }

    @Override
    public ImHttpComm.ImResult setAttr(String[] to_account, String[] attrNames, String[] attrValues) {
        Map<String, Object> params = new HashMap<>();

        List<Map> users = new ArrayList<>();

        for(int j = 0; j < to_account.length; j++){
            Map<String,String> attrs = new HashMap<>();
            for(int i = 0; i < attrNames.length; i++){
                attrs.put(attrNames[i], attrValues[i]);
            }

            Map<String,Object> user = new HashMap<>();
            user.put("To_Account", to_account[j]);
            user.put("Attrs", attrs);

            users.add(user);

        }

        params.put("UserAttrs", users);
        return imHttpComm.post("/all_member_push/im_set_attr", params);
    }

    @Override
    public ImHttpComm.ImResult removeAttr(String[] to_accounts, String[] attrNames) {
        Map<String, Object> params = new HashMap<>();

        List<Map> users = new ArrayList<>();

        for(int j = 0; j < to_accounts.length; j++){
            Map<String,Object> user = new HashMap<>();
            user.put("To_Account", to_accounts[j]);
            user.put("Attrs", attrNames);

            users.add(user);

        }

        params.put("UserAttrs", users);
        return imHttpComm.post("/all_member_push/im_remove_attr", params);
    }

    @Override
    public ImHttpComm.ImResult getTag(String[] to_accounts) {
        Map<String, Object> params = new HashMap<>();
        params.put("To_Account", to_accounts);
        return imHttpComm.post("/all_member_push/im_get_tag", params);
    }

    @Override
    public ImHttpComm.ImResult removeTag(String[] to_accounts, String[] tags) {
        Map<String, Object> params = new HashMap<>();

        List<Map> users = new ArrayList<>();

        for(int j = 0; j < to_accounts.length; j++){
            Map<String,Object> user = new HashMap<>();
            user.put("To_Account", to_accounts[j]);
            user.put("Tags", tags);

            users.add(user);

        }

        params.put("UserTags", users);
        return imHttpComm.post("/all_member_push/im_remove_tag", params);
    }

    @Override
    public ImHttpComm.ImResult removeAllTag(String[] to_accounts) {
        Map<String, Object> params = new HashMap<>();

        params.put("To_Account", to_accounts);
        return imHttpComm.post("/all_member_push/im_remove_all_tags", params);
    }
}
